import 'package:erp/data/model/process_node.dart';
import 'package:erp/res/colors.dart';
import 'package:erp/res/resources.dart';
import 'package:erp/widgets/form1/form_builder.dart';
import 'package:erp/widgets/my_app_bar.dart';
import 'package:erp/widgets/my_button.dart';
import 'package:erp/widgets/sale_project_title.dart';
import 'package:flutter/material.dart';
import 'package:flutter_picker/Picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'controller.dart';

export 'binding.dart';
export 'controller.dart';

/// 流程节点人员审批选择
class ProcessNodeChoosePage extends StatelessWidget {
  getData(List<PickerItem> items, List<int> indexList) {
    assert(indexList.isNotEmpty, 'indexList cannot be empty');
    int index = indexList.removeAt(0);

    if (indexList.isEmpty) {
      return (items[index].text as Text).data;
    }
    return getData(items[index].children!, indexList);
  }

  _buildNode(BuildContext context, ProcessNode node, List<PickerItem> items) {
    var _controller = TextEditingController();
    return Container(
      color: Colors.white,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(height: 30.h),
          Padding(
            padding: EdgeInsets.only(left: 52.w),
            child: Text(
              node.nodeName,
              style: TextStyle(fontSize: 28.sp, color: Color(0xff666666)),
            ),
          ),
          SizedBox(height: 30.h),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 32.w),
            child: Divider(
              height: 1.w,
              thickness: 1.w,
            ),
          ),
          Container(
            height: 90.h,
            padding: EdgeInsets.symmetric(horizontal: 52.w),
            child: LabelWrapper(
              label: '* 审批人员',
              child: GestureDetector(
                onTap: () {
                  Picker picker = Picker(
                      adapter: PickerDataAdapter(data: items),
                      changeToFirst: true,
                      textAlign: TextAlign.left,
                      height: 220,
                      itemExtent: 38,
                      columnPadding: const EdgeInsets.all(8.0),
                      onConfirm: (Picker tmp, List value) {
                        _controller.text =
                            getData((tmp.adapter as PickerDataAdapter).data, List.from(value as List<int>));
                        node.nodeOrgId = tmp.getSelectedValues()[0];
                        node.nodeEmpId = tmp.getSelectedValues()[1];
                      });
                  picker.showModal(context);
                },
                child: Row(
                  children: [
                    Expanded(
                      child: TextField(
                        controller: _controller,
                        enabled: false,
                        minLines: 1,
                        textAlign: TextAlign.right,
                        style: Styles.genNormalTextStyle(color: Colours.FORM_FIELD_VALUE_COLOR),
                        decoration: InputDecoration(
                          isDense: true,
                          contentPadding: EdgeInsets.all(0),
                          alignLabelWithHint: true,
                          border: Styles.NONE_INPUT_BORDER,
                          enabledBorder: Styles.NONE_INPUT_BORDER,
                          focusedBorder: Styles.NONE_INPUT_BORDER,
                          hintText: '请选择',
                          hintStyle: TextStyle(
                            color: Colours.PLACE_HOLDER_COLOR,
                            fontSize: 26.sp,
                            height: 1.6,
                          ),
                        ),
                      ),
                    ),
                    SizedBox(width: 10.w),
                    Offstage(
                      offstage: false,
                      child: Container(
                        child: Image.asset(
                          'assets/images/right_arrow.png',
                          width: 12.w,
                          height: 20.h,
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    var controller = Get.find<ProcessNodeChooseController>();
    return Scaffold(
      backgroundColor: Colours.bg_color,
      appBar: MyAppBar(
        titleColor: Colours.text,
        centerTitle: "流程人员选择",
      ),
      body: Container(
        child: Column(
          children: [
            SaleProjectTile(title: '选择流程审批人', padding: EdgeInsets.only(left: 32.w)),
            ...controller.items.entries.map(
              (e) => _buildNode(context, e.key, e.value),
            ),
            SizedBox(height: 30.h),
            MyButton(
              minWidth: 600.w,
              minHeight: 80.h,
              textColor: Colors.white,
              fontSize: 34.sp,
              text: '确定',
              radius: 40.r,
              onPressed: () {
                controller.submit();
              },
            )
          ],
        ),
      ),
    );
  }
}
